package main;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class UsbDiscover {

	static final long MAXIMUM_PROBABLE_DRIVE_SIZE_MEGABYTES = 128 *1024*1024;
	static final int MAXIMUM_PROBABLE_NUMBER_FILES = 300000000;
	static final int MAXIMUM_PROBABLE_NUMBER_RECORDINGS= 100000000;
	static  char[] NON_USB_DRIVES = {'c' , 'd', 'C','D'};
 

	public static ArrayList<File> getUSBWAVFiles() {

		ArrayList<File> returnList = new ArrayList<File>();
 
		for(File f :File.listRoots()) {
			boolean reject = false;
			for(char c :NON_USB_DRIVES) {
				char t = f.getAbsolutePath().charAt(0);
				if(t == c || !f.isDirectory()) {
				reject = true;
					break;}
				}

			if(f.getTotalSpace()/ 1024 > MAXIMUM_PROBABLE_DRIVE_SIZE_MEGABYTES || reject) {
				continue;
			}
			Integer totalFilesInDir = new Integer(0);
			String[] extensionToFilter = {"wav"};
			
//			if (totalFilesInDir < MAXIMUM_PROBABLE_NUMBER_FILES ||returnList.size() > MAXIMUM_PROBABLE_NUMBER_RECORDINGS)
//				{
//				continue;
//				}
			returnList.addAll(getFilesMatchExtensionDriveFromPath(f.getAbsolutePath(),extensionToFilter , totalFilesInDir));


		}


		return returnList;


	}

	public  static ArrayList<File> getFilesMatchExtensionDriveFromPath(String folderPath, String extensionList[], Integer totalNumberFiles) {

		totalNumberFiles  = 0;

		Queue<File> directoriesToLookAt = new LinkedList<File>();

		ArrayList<File> filesMatchingExtensions = new ArrayList<File>();

		File temp = new File(folderPath);

		directoriesToLookAt.add(temp);

		while (!directoriesToLookAt.isEmpty()) {

			File current = directoriesToLookAt.poll();

			for (File t : current.listFiles()) {
				if (t.isDirectory()) {
					directoriesToLookAt.add(t);
				} else {
					totalNumberFiles++;
					
					String extension = t.getName().substring(t.getName().length() - 3);
					for (String s : extensionList)

						if (s.compareToIgnoreCase(extension) == 0) {
							filesMatchingExtensions.add(t);
							break;
						}
				}
			}

		}

		return filesMatchingExtensions;

	}
}
